package com.ruoyi.xiaoshou.controller;

import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.page.TableDataInfo;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.RequiresPermissions;
import com.ruoyi.xiaoshou.domain.SalesmanReport;
import com.ruoyi.xiaoshou.service.ISalesmanReportService;
import net.sf.jasperreports.engine.JRException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.io.FileNotFoundException;
import java.util.List;

/**
 * 销售业务员报表Controller
 *
 * @author wl
 * @date 2024-11-08
 */
@RestController
@RequestMapping("/salesmanReport")
public class SalesmanReportController extends BaseController {
    @Autowired
    private ISalesmanReportService salesmanReportService;

    /**
     * 查询销售业务员报表列表
     */
    @RequiresPermissions("xiaoshou:salesmanReport:list")
    @GetMapping("/list")
    public TableDataInfo list(SalesmanReport salesmanReport) {
        startPage();
        List<SalesmanReport> list = salesmanReportService.selectSalesmanReportList(salesmanReport);
        return getDataTable(list);
    }

    /**
     * 导出销售业务员报表列表
     */
    @RequiresPermissions("xiaoshou:salesmanReport:export")
    @Log(title = "销售业务员报表", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, SalesmanReport salesmanReport) {
        List<SalesmanReport> list = salesmanReportService.selectSalesmanReportList(salesmanReport);
        ExcelUtil<SalesmanReport> util = new ExcelUtil<SalesmanReport>(SalesmanReport.class);
        util.exportExcel(response, list, "销售业务员报表数据");
    }

    /**
     * @return void
     * @description 导出供应商统计PDF
     * @date 2024/11/15 上午11:46
     * @Param response:
     * @Param buyerReport:
     **/
    @Log(title = "销售员销售报表pdf", businessType = BusinessType.EXPORT)
    @PostMapping("/print")
    public ResponseEntity<byte[]> print(HttpServletResponse response, SalesmanReport salesmanReport) throws JRException, FileNotFoundException {
        HttpHeaders httpHeaders = new HttpHeaders();
        httpHeaders.setContentType(MediaType.APPLICATION_OCTET_STREAM);
        return new ResponseEntity<>(salesmanReportService.printSalesmanReport(salesmanReport), httpHeaders, HttpStatus.OK);
    }

}
